package com.sinoyd.itszyw.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sinoyd.itszyw.Adapter.SelectAdapter;
import com.sinoyd.itszyw.Model.IssueInfo;
import com.sinoyd.itszyw.R;

import java.util.List;

/**
 * @author: Sinoyd  --------->作者
 * @description: --------->描述
 * @projectName: ITSZYW  ---------> 项目名称
 * @date: 2017-03-10 ---------> 创建日期
 * @time: 15:51       ---------> 创建时间
 */
public class SelectDialog extends Dialog implements AdapterView.OnItemClickListener {

    private List arrryList;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private SelectAdapter mAdapter;
    private ListView list_pop;
    public  static int mCurrentPos;
    private SelectTaskTypeListener typeListener;

    private SelectTaskRepairsListener repairsListener;

    private SelectTaskStateListener stateListener;

    private SelectTaskIssuerListener issuerListener;

    private SelectInspectionResultListener inspectionResultListener;


    public SelectDialog(Context context, List list) {
        super(context);
        this.mContext = context;
        this.arrryList = list;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public SelectDialog(Context context,List list ,@StyleRes int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        this.arrryList = list;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public interface SelectTaskTypeListener {
        void selectTaskType(String str);

        void changeArrowDirection();

    }

    public void setSelectTaskTypeListener(SelectTaskTypeListener selectTaskTypeListener) {
        this.typeListener = selectTaskTypeListener;
    }


    public interface SelectTaskRepairsListener {
        void selectTaskRepairs(String str);

        void changeArrowDirection();
    }

    public void setSelectTaskRepairsListener(SelectTaskRepairsListener selectTaskRepairsListener) {
        this.repairsListener = selectTaskRepairsListener;
    }

    public interface SelectTaskStateListener {
        void selectTaskState(String str);

        void changeArrowDirection();
    }

    public void setSelectTaskStateListener(SelectTaskStateListener selectTaskStateListener) {
        this.stateListener = selectTaskStateListener;
    }

    public  interface SelectTaskIssuerListener{

        void selectTaskIssuer(IssueInfo issueInfo);
    }

    public void setSelectTaskIssuerListener(SelectTaskIssuerListener selectTaskIssuerListener){
        this.issuerListener = selectTaskIssuerListener;
    }


    public interface SelectInspectionResultListener{
        void selectInspectionResult(String str,int id);
    }
    public void setSelectInspectionResultListener(SelectInspectionResultListener selectInspectionResultListener){
        this.inspectionResultListener = selectInspectionResultListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(arrryList.get(0).equals("异常") ? R.layout.dialog_pop_result:R.layout.dialog_pop_list);
        list_pop = (ListView) findViewById(R.id.list_pop);
        list_pop.setDivider(null);//去掉条目间的分割线
        list_pop.setCacheColorHint(Color.TRANSPARENT);//让ListView滑动的时候条目背景色不会变成黑色
        list_pop.setSelector(new ColorDrawable(mContext.getResources().getColor(R.color.color_gray_lighter)));//设置默认状态选择器为全透明，不传颜色就是没颜色（也就是条目被点击时，背景没颜色）
        mAdapter = new SelectAdapter(mContext, arrryList);
        list_pop.setAdapter(mAdapter);
        list_pop.setOnItemClickListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

        mCurrentPos = i;

        if (typeListener != null) {
            typeListener.selectTaskType(arrryList.get(i).toString());

        }

        if (repairsListener != null) {
            repairsListener.selectTaskRepairs(arrryList.get(i).toString());
        }

        if (stateListener != null) {
            stateListener.selectTaskState(arrryList.get(i).toString());
        }

        if (issuerListener != null){
            issuerListener.selectTaskIssuer((IssueInfo) arrryList.get(i));
        }
        if (inspectionResultListener != null){
            inspectionResultListener.selectInspectionResult(arrryList.get(i).toString(),i);
        }
//        mAdapter.notifyDataSetChanged();

        dismiss();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (typeListener != null) {
            typeListener.changeArrowDirection();
        }
        if (repairsListener != null) {
            repairsListener.changeArrowDirection();
        }
        if (stateListener != null) {
            stateListener.changeArrowDirection();
        }
        return super.onTouchEvent(event);
    }
}
